// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: "vistcx-i7q7h"
})
const db = cloud.database()
// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const timestamp = Date.parse(new Date())
  const nowDate = new Date(timestamp)
  const year = nowDate.getFullYear();
  const month = (nowDate.getMonth() + 1 < 10 ? '0' + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1);
  const day = (nowDate.getDate() + 1 < 10 ? '0' + nowDate.getDate(): nowDate.getDate());
  const date = year + '-' + month + '-' + day

  return await db.collection('order').where({ _openid: wxContext.OPENID }).orderBy('date', 'desc').get().then(res => {
    for (var i in res.data) {
      if (res.data[i].date >= date) {
        continue
      } else {
        res.data[i].valid = false
      }
    }
      
    return res.data
  })

}